 

import Control.TradeController;
import Model.*;

/**
 * UI for Sales
 * @author Stefan
 */
public class SaleUI {

    private TradeController tradeCtl = TradeController.getInstance();
    private AssociateContainer assCtr = AssociateContainer.getInstance();

    /**
     * Start Sale menu
     */
    public void start()
    {
        saleMenu();
    }

    /**
     * Sale menu
     */
    private void saleMenu()
    {
        boolean exit = false;
        while (!exit)
        {
            int choise = writeSaleMenu();
            if (choise == 1)
            {
                startCreate(CommonUIFunctions.readString("\n write ID: "), CommonUIFunctions.readString("\n write Employee ID: "));
            }
            else if (choise == 2)
            {
                startRead(CommonUIFunctions.readString("\n write ID: "));
            }
            else if (choise == 3)
            {
                startAddCustomer(CommonUIFunctions.readString("\n write ID: "));
            }
            else if (choise == 4)
            {
                startAddItem(CommonUIFunctions.readString("\n write ID: "));
            }
            else
            {
                exit = true;
            }
        }
    }

    /**
     * Print sale menu
     * @return int based on your choise
     */
    private int writeSaleMenu()
    {
        System.out.println("\f *** Sale Menu ***");
        System.out.println(" (1) Create");
        System.out.println(" (2) Read");
        System.out.println(" (3) Add Customer");
        System.out.println(" (4) Add product");
        System.out.println(" (0) Back");

        return CommonUIFunctions.readInt("\n Make your choise: ");
    }

    /**
     * Create menu
     * @param id
     * @param employeeID
     */
    private void startCreate(String id, String employeeID)
    {
        if (tradeCtl.findTrade(id) != null)
        {
            System.out.println("Trade ID is already in use: " + id);
        }
        else if(assCtr.findEmployeeID(employeeID) == null)
        {
            System.out.println("Employee ID doesn't exists: " + employeeID + "\n Security breach! silent alarm is activated!");
        }
        else
        {
            tradeCtl.addTrade(id, employeeID);
        }
    }

    /**
     * Read menu
     * @param ID
     */
    private void startRead(String ID)
    {
        try
        {
            getInfo(ID);
        } catch (Exception e)
        {
            System.out.println("There was no sale with this ID: " + (ID));
        }
    }

    /**
     * Add item menu
     * @param ID
     */
    private void startAddItem(String ID)
    {
        if (tradeCtl.findTrade(ID) != null)
        {
            if (tradeCtl.addSaleItem(CommonUIFunctions.readString("\n write ID of product: "), CommonUIFunctions.readInt("\n write quantity: "), ID) == false)
            {
                System.out.println("check out the products in stock");
            }
        }
        else
        {
            startAddItem(CommonUIFunctions.readString("There was no sale with this ID:" + ID + "\n try another ID: "));
        }
    }

    /**
     * Add Customer menu
     * @param ID
     */
    private void startAddCustomer(String ID)
    {
        if (tradeCtl.findTrade(ID) != null)
        {
            //If false write error message
            if (tradeCtl.addCustomer(CommonUIFunctions.readString("\n write ID of Customer: "), ID) == false)
            {
                System.out.println("Customer ID doesnt exists. Try again.");
            }
        }
        else
        {
            startAddCustomer(CommonUIFunctions.readString("There was no sale with this ID:" + ID + "\n try another ID: "));
        }
    }

    /**
     * Get info about Sale
     * @param ID
     */
    private void getInfo(String ID)
    {
        Trade trade = tradeCtl.findTrade(ID);
        System.out.println("**Sale**");
        System.out.println("ID: " + trade.getId());
        for (SalesLineItem item : trade.getSaleLines())
        {
            System.out.println(item.getProduct().getName() + ": " + item.getQuantity() + " / " + item.getProduct().getPrice() + " = " + item.subTotalPrice());
        }
        System.out.println("Employee: " + assCtr.findEmployeeID(trade.getEmployeeID()).getName());
        if (trade.getCustomerID() != null)
        {
            System.out.println("Customer: " + assCtr.findCustomerID(trade.getCustomerID()).getName());
        }
        System.out.println("Total price: " + trade.totalPrice());
    }
}
